package com.ruoyi.common.oms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.oms.dao.YytOmsCartItemMapper;
import com.ruoyi.common.oms.dao.YytOmsOrderItemMapper;
import com.ruoyi.common.oms.entity.YytOmsCartItem;
import com.ruoyi.common.oms.service.YytOmsCartItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 购物车Service类
 *
 * @author tanxingxian
 * @date 2021-12-22 14:35
 */
@Service
public class YytOmsCartItemServiceImpl extends ServiceImpl<YytOmsCartItemMapper, YytOmsCartItem> implements YytOmsCartItemService {

    @Autowired
    private YytOmsOrderItemMapper yytOmsOrderItemMapper;

    @Autowired
    private YytOmsCartItemMapper yytOmsCartItemMapper;

    /**
     * 添加购物车
     */
    @Override
    public void addShopCart(YytOmsCartItem cartItem) {
        LambdaQueryWrapper<YytOmsCartItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(YytOmsCartItem::getMemberId, cartItem.getMemberId());
        wrapper.eq(YytOmsCartItem::getProductSkuId, cartItem.getProductSkuId());
        YytOmsCartItem dbCartItem = getOne(wrapper);
        if (null != dbCartItem) {
            dbCartItem.setQuantity(dbCartItem.getQuantity() + 1);
            dbCartItem.setUpdateBy(cartItem.getCreateBy());
            updateById(dbCartItem);
        } else {
            save(cartItem);
        }
    }

    @Override
    public Integer getOrderInfoType(String orderId, String memberId) {
        return yytOmsOrderItemMapper.getOrderInfoType(orderId, memberId);
    }

    @Override
    public Map getCartNumAndMoney(String id) {
        return yytOmsOrderItemMapper.cartNumAndMoney(id);
    }
}
